package ua.com.minus1.lookbattle;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockDialogFragment;
import ua.com.minus1.lookbattle.models.BonusConvertResult;
import ua.com.minus1.lookbattle.net.HttpManager;
import ua.com.minus1.lookbattle.net.URLManager;
import ua.com.minus1.lookbattle.utils.AppPreferences;
import ua.com.minus1.lookbattle.utils.BundleValues;
import ua.com.quietlycoding.android.picker.NumberPicker;

/**
 * Created with IntelliJ IDEA.
 * User: Flash
 * Date: 02.10.12
 * Time: 11:47
 * To change this template use File | Settings | File Templates.
 */
public class ConvertDialogFragment extends SherlockDialogFragment implements View.OnClickListener, NumberPicker.OnChangedListener {

    private NumberPicker mPicker;
    private TextView mClicksText;

    private int mBonuses;
    private int mClicks;
    private int mLookId;
    private int mCatId;

    private final int CONVERT_FACTOR = 1;
    private Callbacks mCallbacks;
    private HttpManager mHttpManager;
    private String mUuid;

    public interface Callbacks{
        void onConvert(int bonuses, int clicks);
    }

    public static ConvertDialogFragment newInstance(int catId, int lookId, int bonuses, int clicks) {
        Bundle args = new Bundle();
        args.putInt(BundleValues.CAT_ID, catId);
        args.putInt(BundleValues.LOOK_ID, lookId);
        args.putInt(BundleValues.BONUSES, bonuses);
        args.putInt(BundleValues.CLICKS, clicks);

        ConvertDialogFragment fragment = new ConvertDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mCallbacks = (Callbacks) getTargetFragment();
        mHttpManager = new HttpManager(activity);
        AppPreferences preferences = new AppPreferences(activity);
        mUuid = preferences.getUUID();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle args = getArguments();
        mCatId = args.getInt(BundleValues.CAT_ID);
        mLookId = args.getInt(BundleValues.LOOK_ID);
        mBonuses = args.getInt(BundleValues.BONUSES);
        mClicks = args.getInt(BundleValues.CLICKS);

        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_dialog_convert, container, false);
        mPicker = (NumberPicker) viewGroup.findViewById(R.id.convertNumberPicker);
        mClicksText = (TextView) viewGroup.findViewById(R.id.convertClicks);
        Button mConvertBtn = (Button) viewGroup.findViewById(R.id.convertDialogConvertBtn);
        mConvertBtn.setOnClickListener(this);
        Button mCancelBtn = (Button) viewGroup.findViewById(R.id.convertDialogCancelBtn);
        mCancelBtn.setOnClickListener(this);

        mPicker.setRange(0, (int) Math.floor(mClicks * CONVERT_FACTOR));
        mPicker.setCurrent(mBonuses);
        mPicker.setOnChangeListener(this);
        updateClicks(mClicks);

        getDialog().setTitle("Convert");

        return viewGroup;
    }

    public void onChanged(NumberPicker picker, int oldVal, int newVal) {
        updateClicks(mClicks - (newVal - mBonuses) / CONVERT_FACTOR);
    }

    private void updateClicks(int clicks) {
        mClicksText.setText(String.valueOf(clicks));
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.convertDialogConvertBtn:
                convert();
                break;
            case R.id.convertDialogCancelBtn:
                dismiss();
        }
    }

    public void convert() {
        int bonuses = mPicker.getCurrent() - mBonuses;
        String url = bonuses > 0 ? URLManager.getBonusUpUrl(mUuid, mCatId, mLookId, bonuses) : URLManager.getBonusRefundUrl(mUuid, mCatId, mLookId, Math.abs(bonuses));
        Log.d(ConvertDialogFragment.class.getName(), "request for bonus update catId: " + mCatId + " lookId: " + mLookId + " bonuses: " + bonuses + " url: " + url);
        mHttpManager.loadClass(url, BonusConvertResult.class, new HttpManager.HttpResultHandler<BonusConvertResult>() {
            public void onResultLoadComplete(BonusConvertResult result) {
                Log.i(ConvertDialogFragment.class.getName(), "bonus convert result: " + result.toString());
                mCallbacks.onConvert(result.bonuses, result.clicks);
            }
        });
        dismiss();
    }
}
